//数据库操作对象
var DbBase = require("../models/db/DbBase");
var url = require('url');
//站点配置
var AdminFunc = require("../models/db/adminFunc");
var settings = require("../models/db/settings");
//材料
var Ingredient = require("../models/Ingredient");
var Cuisine = require("../models/Cuisine");
var Setmeal = require("../models/Setmeal");
var Purchase = require("../models/Purchase");
var CashFlow = require("../models/CashFlow");


//短id
var shortid = require('shortid');
var util=require('../base/Util');
var express = require('express');
var router = express.Router();


//对象列表查询
router.get('/manage/getDocumentList/:defaultUrl',function(req,res,next){
	var currentPage = req.params.defaultUrl;
	var queryJson={};

	if(AdminFunc.checkAdminPower(req,currentPage + '_view')){
		var conditions={};
		var targetObj = AdminFunc.getTargetObj(currentPage);
		var params = url.parse(req.url,true);
		var keywords = params.query.searchKey;
		var startDate = params.query.startDate;
		var endDate = params.query.endDate;
		var keyPr = [];
		if(keywords&&keywords!=""){
			var reKey = new RegExp(keywords, 'i');
			keyPr.push({'name' : { $regex: reKey } });
		}
		if(targetObj == CashFlow){
			var andArray=[];
			var todayStr=new Date().Format("yyyy-MM-dd");
			startDate=startDate==null?todayStr:startDate;
			endDate=endDate==null?todayStr:endDate;
			var dateCondition={};
			var tmp={};
			tmp["$gte"]=startDate+" 00:00:00";
			dateCondition["time"]=tmp;
			andArray.push(dateCondition);
			dateCondition={};
			tmp={};
			tmp["$lte"]=endDate+" 23:59:59";
			dateCondition["time"]=tmp;
			andArray.push(dateCondition);
			queryJson["$and"]=andArray;
		}
		DbBase.pagination(targetObj,req, res,keyPr,queryJson);
	}else{
		return res.json({});
	}
});

//更新单条记录(执行更新)
router.post('/manage/:defaultUrl/modify',function(req,res,next){


	var currentPage = req.params.defaultUrl;
	var targetObj = AdminFunc.getTargetObj(currentPage);
	var params = url.parse(req.url,true);
	if(targetObj==Ingredient||targetObj==Cuisine||targetObj==Setmeal){
		if(req.body.name) {
			req.body.sx = util.getPinYinSx(req.body.name);
		}
	}
	if(AdminFunc.checkAdminPower(req,currentPage + '_modify')){
		DbBase.updateOneByID(targetObj,req, res,"update one obj success")
	}else{
		res.end('对不起，您无权执行该操作！');
	}
});


//对象新增
router.post('/manage/:defaultUrl/addOne',function(req,res,next){

	var currentPage = req.params.defaultUrl;
	req.body.time=req.body.time==null?new Date():req.body.time;
	console.log("ttes"+req.body.time);

	var targetObj = AdminFunc.getTargetObj(currentPage);
	if(targetObj==Ingredient||targetObj==Cuisine||targetObj==Setmeal){
		req.body.sx=util.getPinYinSx(req.body.name);
	}

	if(AdminFunc.checkAdminPower(req,currentPage + '_add')){
		DbBase.addOne(targetObj,req, res);
	}else{
		res.end('对不起，您无权执行该操作！');
	}

});

router.get('/manage/:defaultUrl/del',function(req,res,next){
	var currentPage = req.params.defaultUrl;
	var params = url.parse(req.url,true);
	var targetObj = AdminFunc.getTargetObj(currentPage);
	if(AdminFunc.checkAdminPower(req,currentPage + '_del')){
		DbBase.del(targetObj,req,res,"del one obj success");
	}else{
		res.end('对不起，您无权执行该操作！');
	}

});

//批量删除对象
router.get('/manage/:defaultUrl/batchDel',function(req,res,next){
	var currentPage = req.params.defaultUrl;
	var params = url.parse(req.url,true);
	var targetObj = AdminFunc.getTargetObj(currentPage);
	var ids = params.query.ids;
	if(AdminFunc.checkAdminPower(req,currentPage + '_del')){
		var idsArr = ids.split(',');
		if(idsArr.length > 0){
			if(false){
				res.end('对不起，该模块不允许批量删除！');
			}else{
				targetObj.remove({'_id':{$in: idsArr}},function(err){
					if(err){
						res.end(err);
					}else{
						res.end("success");
					}
				});

			}
		}else{
			res.end('请选择至少一项后再执行删除操作！');
		}

	}else{
		res.end('对不起，您无权执行该操作！');
	}

});

//获取单个对象数据
router.get('/manage/:defaultUrl/item',function(req,res,next){
	var currentPage = req.params.defaultUrl;
	var targetObj = AdminFunc.getTargetObj(currentPage);
	if(AdminFunc.checkAdminPower(req,currentPage + '_view')){
		DbBase.findOne(targetObj,req, res,"find one obj success");
	}else{
		return res.json({});
	}

});


//材料管理列表
router.get('/manage/ingredientList', function(req, res, next) {
	AdminFunc.renderToManagePage(req, res,'manage/ingredientList',settings.INGREDIENT);
});

//菜式管理列表
router.get('/manage/cuisineList', function(req, res, next) {
	AdminFunc.renderToManagePage(req, res,'manage/cuisineList',settings.CUISINE);
});

//套餐管理列表
router.get('/manage/setmealList', function(req, res, next) {
	AdminFunc.renderToManagePage(req, res,'manage/setmealList',settings.SETMEAL);
});

//入货管理列表
router.get('/manage/purchaseList', function(req, res, next) {
	AdminFunc.renderToManagePage(req, res,'manage/purchaseList',settings.PURCHASE);
});

//销售管理列表
router.get('/manage/saleList', function(req, res, next) {
	AdminFunc.renderToManagePage(req, res,'manage/saleList',settings.SALE);
});

//资金流动列表
router.get('/manage/cashflowList', function(req, res, next) {
	AdminFunc.renderToManagePage(req, res,'manage/cashflowList',settings.CashFlow);
});



module.exports = router;